//
//  TLMallListViewController.m
//  WXReader
//
//  Created by huaya on 2025/1/13.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLMallListViewController.h"
#import "TLMallListTableViewCell.h"
#import "TLMallListBannerTableViewCell.h"
#import "TLShortStoryViewController.h"
#import "TLMallChannelModel.h"

@interface TLMallListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic, assign) int pageNumb;
@property (nonatomic, assign) int pageSize;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation TLMallListViewController
- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray new];
    }
    return _dataArr;
}
- (void)resetFrame:(CGRect)frame{
    self.view.frame = frame;
    
    _tableView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNumb = 1;
    self.pageSize = 10;
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
    [self network];
}
#pragma mark - table delegate - datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
    if (indexPath.row>=self.dataArr.count) {
        return [[UITableViewCell alloc] init];
    }
    
    TLMallChannelModel *model = self.dataArr[indexPath.row];
    
    if ([model.showType intValue] == TLShowTypeBanner) {
        TLMallListBannerTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TLMallListBannerTableViewCell"];
        
        cell.bannerList = model.bannerList;
        
        return cell;
    }else{
        TLMallListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TLMallListTableViewCell"];
        cell.model = model;
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TLMallChannelModel *model = self.dataArr[indexPath.row];
    
    if ([model.worksType isEqualToString:@"0"]) {
        //短篇
        TLShortStoryViewController *vc = [TLShortStoryViewController new];
        vc.worksId = model.worksId;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        //长篇
//        TL_BookMallDetailViewController *vc = [TL_BookMallDetailViewController new];
//        vc.book_id = [model.worksId intValue];
//        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
- (UITableView *)tableView{
    if (!_tableView) {
        
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,MK_SCREEN_SAVE_BOTTOM_HEIGHT)];
        footView.backgroundColor = UIColor.clearColor;
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = footView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadLastData)];
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        
        if (@available(iOS 15.0, *)) {
            self.tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.frame = self.view.bounds;
        
        [_tableView registerNib:[UINib nibWithNibName:@"TLMallListBannerTableViewCell" bundle:nil] forCellReuseIdentifier:@"TLMallListBannerTableViewCell"];
        
        [_tableView registerNib:[UINib nibWithNibName:@"TLMallListTableViewCell" bundle:nil] forCellReuseIdentifier:@"TLMallListTableViewCell"];
        
    }
    return _tableView;
}
- (void)loadMoreData{
    
    self.pageNumb = self.pageNumb + 1;
    
    [self network];
    
}
- (void)loadLastData{
    
    self.pageNumb = 1;
    [self.dataArr removeAllObjects];
    [self network];
    
}
- (void)network{
    
    [APIManager sendGetRequest:[APIURLManager queryOsWorksByChannel] parameters:@{@"channelId":@(self.channelId),@"pageNum":@(self.pageNumb),@"pageSize":@(self.pageSize)} model:nil success:^(BOOL success, id  _Nullable responseData, APIResponse * _Nonnull requestModel) {
        
        if (success) {
            
            NSArray *dataArr = [TLMallChannelModel mj_objectArrayWithKeyValuesArray:requestModel.data[@"records"]];
            
            [self.dataArr addObjectsFromArray:dataArr];
            
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            if (dataArr.count<self.pageSize) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        
        NSLog(@"123:%@",error);
        
    }];
    
}
@end
